package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetaillnfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "01.资讯管理模块")

@Slf4j  //简化打印输出代码
@RestController
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;


    @PostMapping("add")
    @ApiOperation("添加资讯")
    @ApiOperationSupport(order = 10)
    public JsonResult add(NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam = " + noticeAddParam);
        /*
            梳理业务,确定接口;
            以注册功能为例
            1.接收客户端传递过来的数据;
            2.校验数据的合法性;
            3.校验用户名是否被占用[SELECT ... FROM user WHERE username=?];
              3.1 被占用: 直接返回用户名被占用;
              3.2 未被占用: 在用户表中插入1条数据,返回注册成功[INSERT INTO user VALUES(...)];
         */
        Notice notice = new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeAddParam, notice);
        log.debug("notice = " + notice);
        //处理其他属性
        notice.setUserId(100L);//暂时写死
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("notice2 = " + notice);

        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }

    @GetMapping("list")
    @ApiOperation("资讯列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(NoticeAddQuery noticeAddQuery){
        log.debug("noticeAddQuery=" + noticeAddQuery);
        //SELECT ... FROM notice WHERE title=? AND type=?
        String title = noticeAddQuery.getTitle();
        Integer type = noticeAddQuery.getType();
        List<NoticeListVO> noticeListVOS = noticeMapper.list(title,type);

        return JsonResult.ok(noticeListVOS);
    }



    @PostMapping("update")
    @ApiOperation("更新资讯")

    public JsonResult update(NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam = " + noticeAddParam);

        //接收一个NoticeAddParam类型的参数noticeAddParam。
        Notice notice = new Notice();

        //使用BeanUtils.copyProperties方法将noticeAddParam的属性复制到Notice对象notice中。
      BeanUtils.copyProperties(noticeAddParam, notice);

     //更新时间设置：将notice对象的更新时间设置为当前时间。
       notice.setUpdateTime(new Date());
//数据库更新：调用noticeMapper的update方法更新数据库中的资讯信息，
// 返回更新的记录数。
       int i = noticeMapper.update(notice);
       if(i > 0){
           return JsonResult.ok();


       }else {
           return new JsonResult(StatusCode.OPERATION_FAILED,update(null));
       }
    }
    @GetMapping("detail")
    @ApiOperation("资讯详情")
    @ApiOperationSupport(order = 30)
   @ApiImplicitParams(value = {
           @ApiImplicitParam(name = "id", value = "资讯编号", required = true, dataType = "Long"),
           @ApiImplicitParam(name = "id", value = "资讯标题", required = true,dataType = "String")
   })
           public JsonResult noticeDetail(Long id){
        log.debug("id = " + id);
       NoticeDetaillnfoVO noticeDetaillnfoVO = noticeMapper.noticeDetail(id);
       return JsonResult.ok(noticeDetaillnfoVO);
    }

    @PostMapping("del")
    @ApiOperation("删除资讯")

    public JsonResult deleteNotice(Long id){
        log.debug("ids = " + id);
       noticeMapper.deleteNotice(id);

        return JsonResult.ok();
    }
}
